/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.mine.presenter;

import android.text.TextUtils;

import com.suozhang.framework.utils.ValidatorUtil;
import com.yiqi.liebang.common.util.RxRegTool;
import com.yiqi.liebang.entity.bo.AllAuthBo;
import com.yiqi.liebang.entity.bo.EducationBo;
import com.yiqi.liebang.entity.bo.UserCardBo;
import com.yiqi.liebang.entity.bo.WorkBo;
import com.yiqi.liebang.feature.mine.contract.CertificationContract;
import com.yiqi.liebang.feature.mine.dependencies.certification.DaggerCertificationPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class CertificationPresenter implements CertificationContract.Presenter {

    private CertificationContract.View mView;

    @Inject

    CertificationContract.Model mModel;


    public CertificationPresenter(CertificationContract.View mView) {
        this.mView = mView;
        DaggerCertificationPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void getUserCardData() {
        mModel.getUserCardData().compose(mView.bindToLife()).subscribe(new Observer<UserCardBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserCardBo userCenterBo) {
                mView.showUserCardSuccess(userCenterBo);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userCertification(String userHead, String userName,
                                  String userLabelId, String userIndustry,
                                  String userWorkAddress,
                                  String userDetailAddress, String userPhone, String userEmail, String wxId,
                                  String cardNumber,
                                  String cardImageUrl) {
        if (!RxRegTool.isMobile(userPhone)) {
            mView.showError("手机号格式不正确！");
            return;
        }
        if (!RxRegTool.isEmail(userEmail)) {
            mView.showError("请输入正确的邮箱！！");
            return;
        }

        if (!ValidatorUtil.isIDCard(cardNumber)) {
            mView.showError("身份证号码格式有误！");
            return;
        }
//        if (TextUtils.isEmpty(cardImageUrl)){
//            mView.showError("请上传手持身份证照片！");
//            return;
//        }


        mModel.userCertification(userHead, userName, userLabelId, userIndustry, userWorkAddress, userDetailAddress, userPhone, userEmail, wxId, cardNumber, cardImageUrl).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showCertificationSuccess();
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userGetWork(String id) {
        mModel.userGetWork(new WorkBo(id)).compose(mView.bindToLife()).subscribe(new Observer<WorkBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(WorkBo s) {
                mView.showWorkSuccess(s);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void authWork(String id, String certificateImage, String licenseImage, String workCardImage, String visitingImage) {
        mModel.authWork(id, certificateImage, licenseImage, workCardImage, visitingImage).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showAuthWorkSuccess();
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userGetEducation(String id) {
        mModel.userGetEducation(new EducationBo(id)).compose(mView.bindToLife()).subscribe(new Observer<EducationBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(EducationBo s) {
                mView.showEducationSuccess(s);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void authEdu(String id, String DiplomaImage, String degreeImage) {
        mModel.authEdu(id, DiplomaImage, degreeImage).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showAuthWorkSuccess();
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getWorkList() {
        mModel.getWorkList().compose(mView.bindToLife())
                .subscribe(new Observer<List<WorkBo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(List<WorkBo> s) {
                        mView.showWorkListSuccess(s);
                        mView.dismissLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e.getMessage());
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void getEduList() {
        mModel.getEduList().compose(mView.bindToLife())
                .subscribe(new Observer<List<EducationBo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(List<EducationBo> s) {
                        mView.showEduListSuccess(s);
                        mView.dismissLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e.getMessage());
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void uploadAuthenticationImage(String authenticationId, String uploadType, String image) {
        mModel.uploadAuthenticationImage(authenticationId, uploadType, image).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showUploadAuthenticationImageSuccess();
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void uploadBasicInfo(String userHead, String userName, String userLabelId, String userIndustry, String userWorkAddress, String userDetailAddress, String userPhone, String userEmail, String wxId, String cardNumber, String uploadType, String image) {
        if (!RxRegTool.isMobile(userPhone)) {
            mView.showError("手机号格式不正确！");
            return;
        }
        if (!RxRegTool.isEmail(userEmail)) {
            mView.showError("请输入正确的邮箱！！");
            return;
        }

        if (!ValidatorUtil.isIDCard(cardNumber)) {
            mView.showError("身份证号码格式有误！");
            return;
        }

        mModel.uploadBasicInfo(userHead, userName, userLabelId, userIndustry, userWorkAddress, userDetailAddress, userPhone, userEmail, wxId, cardNumber, uploadType, image).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showUploadAuthenticationImageSuccess();
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getUserSaveData() {
        mModel.getUserSaveData().compose(mView.bindToLife()).subscribe(new Observer<UserCardBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserCardBo userCenterBo) {
                mView.showUserCardSuccess(userCenterBo);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void toPersonalAuthentication(String educationId, String occupationId) {
        mModel.toPersonalAuthentication(new AllAuthBo(educationId, occupationId))
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showCertificationSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showError(e.getMessage());

                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

}
